package com.youyun56.gps.android.commands;

import java.io.Serializable;

import org.json.JSONObject;

import android.util.Log;

public enum Command implements CommandType,Serializable  {
    EXIT(CommandType.Exit, ExitExecutor.class), AddGood(CommandType.AddGood, AddGoodExecutor.class), GetGoods(
            CommandType.GetGoods, GetGoodExecutor.class), UploadLocation(CommandType.UploadLocation,
            UploadLocation.class), LOGIN(CommandType.Login, null),Add_IM(CommandType.ADD_IM,AddImExecutor.class),AppendGoodEnd(CommandType.Append_Good_END, AppendGoodEndExecutor.class);

    ;
    private Integer id;
    private Class<? extends Task> clazz;
    private JSONObject input;
    private Object output;

    private Command(Integer id, Class<? extends Task> clazz) {
        // TODO Auto-generated constructor stub
        this.id = id;
        this.clazz = clazz;
    }

    public void execute() {

    }

    public static Command getCommand(JSONObject o) {
        try {
            int id = o.getInt("id");
            Command command = getCommand(id);
            if (command != null) {
                command.setInput(o);
                return command;
            } else {
                Log.w("", String.format("Command id %d not found", id));
                return null;
            }
        } catch (Exception e) {
            // TODO: handle exception
            Log.e("get execute exception", e.getMessage(), e);
            return null;
        }

    }

    public static Command getCommand(Integer id) {
        Command[] commands = values();
        for (Command command : commands) {
            if (command.getId() == id)
                return command;
        }
        return null;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Class<? extends Task> getClazz() {
        return clazz;
    }

    public void setClazz(Class<? extends Task> clazz) {
        this.clazz = clazz;
    }

    public JSONObject getInput() {
        return input;
    }

    public void setInput(JSONObject input) {
        this.input = input;
    }

    public Object getOutput() {
        return output;
    }

    public void setOutput(Object output) {
        this.output = output;
    }

//    @Override
//    public int describeContents() {
//        // TODO Auto-generated method stub
//        return 0;
//    }
//
//    @Override
//    public void writeToParcel(Parcel dest, int flags) {
//        // TODO Auto-generated method stub
//        
//    }
}
